package com.android.lovely.ui.fragment

import androidx.fragment.app.activityViewModels
import androidx.lifecycle.lifecycleScope
import androidx.paging.filter
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.android.lovely.R
import com.android.lovely.const.ChatChange
import com.android.lovely.const.ChatList
import com.android.lovely.data.HomeListData
import com.android.lovely.databinding.FragmentHomeListBinding
import com.android.lovely.framework.BaseLoadAdapter
import com.android.lovely.framework.BaseLoadFragment
import com.android.lovely.framework.BaseLoadViewModel
import com.android.lovely.framework.widget.FlexibleContentContainer
import com.android.lovely.ui.adapter.HomeListAdapter
import com.android.lovely.ui.viewmodel.MyCardViewModel
import com.android.lovely.ui.viewmodel.SearchViewModel
import com.kotlin.extensions.bus.FlowBus
import com.kotlin.extensions.bus.flowOf
import com.kotlin.extensions.coroutine.CoroutineExt.launchSafety
import com.kotlin.extensions.dimen.Dimension.dp
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine

/**
 *
 * @author why
 * @date 2025/3/13 12:38
 */
class SearchResultFragment:
    BaseLoadFragment<FragmentHomeListBinding, HomeListData>() {

    private val mViewModel by activityViewModels<SearchViewModel>()
    override fun getViewModel(): BaseLoadViewModel<HomeListData> {
        return mViewModel
    }

    override fun initView() {
        mBinding.rv.addItemDecoration(HomeListItemDecoration(10.dp));
        super.initView()
        lifecycleScope.launchSafety {
            FlowBus.flowOf<ChatChange>().collectLatest {
                mBinding.srlRefresh.isRefreshing = true
                adapter.refresh()
            }
        }
        lifecycleScope.launchSafety{
            mViewModel.resultFlow.collectLatest {
                if (it) {
                    adapter.refresh()
                    getStateLayout().displayProgress()
                }
            }
        }
    }

    override fun setNoContent() {
        getStateLayout().setNoContentIcon(R.drawable.icon_no_search)
        getStateLayout().setNoContentText(getString(R.string.no_search_content))
        getStateLayout().setNoContentButton(getString(R.string.research), true) {
            adapter.refresh()
        }
    }

    override fun getStateLayout(): FlexibleContentContainer {
        return mBinding.flexState
    }

    override fun getSwipeRefreshLayout(): SwipeRefreshLayout {
        return mBinding.srlRefresh
    }

    override fun getRecyclerView(): RecyclerView {
        return mBinding.rv
    }

    override fun getBaseAdapter(): BaseLoadAdapter<HomeListData, *> {
        return HomeListAdapter()
    }

    override suspend fun initData() {
        mViewModel.pager.combine(mViewModel.removedFlow) { data, remove ->
            data.filter { it.id !in remove }
        }.collectLatest {
            adapter.submitData(it)
        }
    }
}